package com.itfreer.print.base;

import java.io.*;
import org.w3c.tidy.Tidy;

/**
 * 定义基于jTidy实现html转换为xhtml格式
 */
public class HtmlToXHtmlUtils {

	/**
	 * 将html转换为xhtml
	 * @param inString
	 * @return
	 */
	public static String toXHtml(String inString) {
		ByteArrayOutputStream tidyOutStream; // 输出流
		try {
			byte[] bs = inString.getBytes();
			String basil = new String(bs, "utf-8");// 将GB2312转化成UTF-8
			ByteArrayInputStream stream = new ByteArrayInputStream(basil.getBytes());
			tidyOutStream = new ByteArrayOutputStream();
			Tidy tidy = new Tidy();
			tidy.setInputEncoding("UTF-8");
			tidy.setQuiet(true);
			tidy.setOutputEncoding("UTF-8");
			tidy.setShowWarnings(false); // 不显示警告信息
			tidy.setIndentContent(true);//
			tidy.setSmartIndent(true);
			tidy.setIndentAttributes(false);
			tidy.setWraplen(1024); // 多长换行
			// 输出为xhtml
			tidy.setXHTML(true);
			tidy.setErrout(new PrintWriter(System.out));
			tidy.parse(stream, tidyOutStream);
			return tidyOutStream.toString();
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ex.printStackTrace();
			return null;
		}
	}
}